#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Utilities.Miscellaneous;
using Utilities.Testing;
using Utilities.Text;

namespace Utilities.Test.Text
{
  public class StringUtilsFixture : TestFixtureBase
  {
    #region Truncate
    [Test]
    public void TruncateTest()
    {
      string truncatedText;

      truncatedText = StringUtils.Truncate("The quick brown fox jumped over the lazy dog.", 20);

      Assert.AreEqual("The quick brown f...", truncatedText);

      truncatedText = StringUtils.Truncate("The quick brown fox jumped over the lazy dog.", 20, "-");

      Assert.AreEqual("The quick brown fox-", truncatedText);

      truncatedText = StringUtils.Truncate("The quick brown fox jumped over the lazy dog.", 45);

      Assert.AreEqual("The quick brown fox jumped over the lazy dog.", truncatedText);

      truncatedText = StringUtils.Truncate("The quick brown fox jumped over the lazy dog.", 44);

      Assert.AreEqual("The quick brown fox jumped over the lazy...", truncatedText);
    }

    [Test]
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Maximum length must be greater than zero.
Parameter name: maximumLength")]
    public void TruncateMaximumLengthTest()
    {
      StringUtils.Truncate("The quick brown fox jumped over the lazy dog.", -5);
    }
    #endregion

    #region IsWhiteSpace
    [Test]
    public void IsWhiteSpaceTest()
    {
      bool result = StringUtils.IsWhiteSpace(@"  
" + StringUtils.Tab + @"
");

      Assert.IsTrue(result);

      result = StringUtils.IsWhiteSpace(@" 
" + StringUtils.Tab + "  s  ");

      Assert.IsFalse(result);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: s")]
    public void IsWhiteSpaceNullTest()
    {
      StringUtils.IsWhiteSpace(null);
    }
    #endregion

    #region ContainsWhiteSpace
    [Test]
    public void ContainsWhiteSpaceTest()
    {
      bool result = StringUtils.ContainsWhiteSpace(@"  
" + StringUtils.Tab + @"
");

      Assert.IsTrue(result);

      result = StringUtils.ContainsWhiteSpace(@" 
" + StringUtils.Tab + "  s  ");

      Assert.IsTrue(result);

      result = StringUtils.IsWhiteSpace(string.Empty);

      Assert.IsFalse(result);

      result = StringUtils.IsWhiteSpace("Pie");

      Assert.IsFalse(result);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: s")]
    public void ContainsWhiteSpaceNullTest()
    {
      StringUtils.ContainsWhiteSpace(null);
    }
    #endregion

    #region EnsureEndsWith
    [Test]
    public void EnsureEndsWithTest()
    {
      string result;

      result = StringUtils.EnsureEndsWith("Pie", ".");

      Assert.AreEqual("Pie.", result);

      result = StringUtils.EnsureEndsWith("Pie.", ".");

      Assert.AreEqual("Pie.", result);

      result = StringUtils.EnsureEndsWith("Pie. ", ".");

      Assert.AreEqual("Pie. ", result);

      result = StringUtils.EnsureEndsWith("Pie. ", ". ");

      Assert.AreEqual("Pie. ", result);

      result = StringUtils.EnsureEndsWith("Pie. ", ";");

      Assert.AreEqual("Pie. ;", result);

      result = StringUtils.EnsureEndsWith("Pie. ", string.Empty);

      Assert.AreEqual("Pie. ", result);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "Value cannot be null." + StringUtils.CarriageReturnLineFeed + "Parameter name: target")]
    public void EnsureEndsWithTargetNullTest()
    {
      StringUtils.EnsureEndsWith(null, ".");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "Value cannot be null." + StringUtils.CarriageReturnLineFeed + "Parameter name: value")]
    public void EnsureEndsWithValueNullTest()
    {
      StringUtils.EnsureEndsWith("Pie.", null);
    }
    #endregion

    #region IsNullOrEmptyOrWhiteSpace
    [Test]
    public void IsNullOrEmptyOrWhiteSpaceTest()
    {
      bool result;

      result = StringUtils.IsNullOrEmptyOrWhiteSpace(null);

      Assert.IsTrue(result);

      result = StringUtils.IsNullOrEmptyOrWhiteSpace(string.Empty);

      Assert.IsTrue(result);

      result = StringUtils.IsNullOrEmptyOrWhiteSpace(StringUtils.Empty);

      Assert.IsTrue(result);

      result = StringUtils.IsNullOrEmptyOrWhiteSpace(@" " + StringUtils.Tab + StringUtils.CarriageReturnLineFeed);

      Assert.IsTrue(result);

      result = StringUtils.IsNullOrEmptyOrWhiteSpace(@" " + StringUtils.Tab + StringUtils.CarriageReturnLineFeed + " _ ");

      Assert.IsFalse(result);
    }
    #endregion

    #region Indent
    [Test]
    public void IndentTest()
    {
      string result;

      result = StringUtils.Indent(@"The quick brown
fox jumps over
the lazy dog.", 4);

      Assert.AreEqual(@"    The quick brown
    fox jumps over
    the lazy dog.", result);

      result = StringUtils.Indent(@"The quick brown
fox jumps over
the lazy dog.", 1, StringUtils.Tab);

      Assert.AreEqual(@"	The quick brown
	fox jumps over
	the lazy dog.", result);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "Value cannot be null." + StringUtils.CarriageReturnLineFeed + "Parameter name: s")]
    public void IndentStringNullTest()
    {
      StringUtils.Indent(null, 1);
    }

    [Test]
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Must be greater than zero." + StringUtils.CarriageReturnLineFeed + "Parameter name: indentation")]
    public void IndentIndentationLessThanZeroTest()
    {
      StringUtils.Indent("Pie.", -1);
    }
    #endregion

    #region Indent
    [Test]
    public void NumberLines()
    {
      string result;

      result = StringUtils.NumberLines(@"The
quick
brown
fox
jumps
over
the
lazy
dog
.");

      Assert.AreEqual(@"   1. The
   2. quick
   3. brown
   4. fox
   5. jumps
   6. over
   7. the
   8. lazy
   9. dog
  10. .", result);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "Value cannot be null." + StringUtils.CarriageReturnLineFeed + "Parameter name: s")]
    public void NumberLinesNullTest()
    {
      StringUtils.Indent(null, 1);
    }
    #endregion

    [Test]
    public void CharAsUnicode()
    {
      string result;

      result = StringUtils.ToCharAsUnicode('a');
      Assert.AreEqual(@"\u0061", result);

      result = StringUtils.ToCharAsUnicode('<');
      Assert.AreEqual(@"\u003c", result);

      result = StringUtils.ToCharAsUnicode('>');
      Assert.AreEqual(@"\u003e", result);

      result = StringUtils.ToCharAsUnicode('\'');
      Assert.AreEqual(@"\u0027", result);

      result = StringUtils.ToCharAsUnicode('\u0000');
      Assert.AreEqual(@"\u0000", result);

      result = StringUtils.ToCharAsUnicode('\t');
      Assert.AreEqual(@"\u0009", result);
    }
  }
}
